package com.fenghongzhang.aidl_client;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.fenghongzhang.aidl_server.IMyAidlInterface;

public class MainActivity extends AppCompatActivity {
    private Button request;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        request = (Button) findViewById(R.id.request);


    }

    public void request(View view) {
        Intent intent = new Intent();
        intent.setAction("com.feng.aidl");
        intent.setPackage("com.fenghongzhang.aidl_server");
        ServiceConnection serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                IMyAidlInterface iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
                try {
                    int add = iMyAidlInterface.add(5, 5);
                    Toast.makeText(MainActivity.this, ""+add, Toast.LENGTH_SHORT).show();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };
        bindService(intent,serviceConnection,BIND_AUTO_CREATE);


    }
}
